<template>
  <div class="app-container">
    <Holiday style="width:400px;" v-if="false"/>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主题名称" prop="subjectName">
        <el-input v-model="queryParams.subjectName" placeholder="请输入主题名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button size="mini" type="primary" icon="el-icon-plus" @click="handleAdd()"
          v-hasPermi="['consultation:subject:edit']">
            新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="subjectList" row-key="subjectId" default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="栏目名称" align="center" prop="subjectName" />
      <el-table-column label="位置" align="center" prop="codeOpt" :formatter="codeOptFormat"/>
      <el-table-column label="状态" align="center" prop="statusOpt" :formatter="statusOptFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="isPermit('consultation:subject:edit')">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['consultation:subject:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
            v-hasPermi="['consultation:subject:edit']">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['consultation:subject:edit']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改咨询栏目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级栏目" prop="parentId" v-if="form.parentId!=0">
          <treeselect :default-expand-level="1" v-model="form.parentId" :options="subjectOptions" :normalizer="normalizer" placeholder="选择上级栏目" class="input_w"/>
        </el-form-item>
        <el-form-item label="栏目名称" prop="subjectName">
          <el-input v-model="form.subjectName" placeholder="请输入栏目名称" class="input_w"/>
        </el-form-item>
        <el-form-item label="区域" prop="codeOpt">
          <el-select v-model="form.codeOpt" placeholder="请选择区域" class="input_w">
            <el-option v-for="dict in codeOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="statusOpt">
          <el-select v-model="form.statusOpt" placeholder="请选择状态" class="input_w">
            <el-option v-for="dict in statusOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"
        v-hasPermi="['consultation:subject:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listSubject,
    getSubject,
    delSubject,
    addSubject,
    updateSubject,
    listExcludeChild,
  } from "@/api/consultation/subject";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "Subject",
    components:{
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 咨询栏目表格数据
        subjectList: [],
        // 部门树选项
        subjectOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态字典
        statusOptOptions: [],
        codeOptOptions:[],
        // 查询参数
        queryParams: {
          subjectName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          subjectName: [{
            required: true,
            message: "栏目名称不能为空",
            trigger: "blur"
          }],
          statusOpt: [{
            required: true,
            message: "状态不能为空",
            trigger: "change"
          }],
          codeOpt: [{
            required: true,
            message: "区域不能为空",
            trigger: "change"
          }]
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("statusOpt").then(response => {
        this.statusOptOptions = response.data;
      });
      this.getDicts("subjectOpt").then(response => {
        this.codeOptOptions = response.data;
      });
    },
    methods: {
      /** 查询咨询栏目列表 */
      getList() {
        this.loading = true;
        listSubject(this.queryParams).then(response => {
          this.subjectList = this.handleTree(response.data, "subjectId");
          this.loading = false;
        });
      },
      /** 转换部门数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.subjectId,
          label: node.subjectName,
          children: node.children
        };
      },
      // 状态字典翻译
      statusOptFormat(row, column) {
        return this.selectDictLabel(this.statusOptOptions, row.statusOpt);
      },
      codeOptFormat(row, column){
        return this.selectDictLabel(this.codeOptOptions, row.codeOpt);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          statusOpt: 1
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        if (row != undefined) {
          this.form.parentId = row.subjectId;
        }
        this.open = true;
        this.title = "添加咨询栏目";
        listSubject().then(response => {
          this.subjectOptions = this.handleTree(response.data, "subjectId");
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const subjectId = row.subjectId || this.ids
        getSubject(subjectId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改咨询栏目";
        });
        listExcludeChild(subjectId).then(response => {
            this.subjectOptions = this.handleTree(response.data, "subjectId");
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.subjectId != null) {
              updateSubject(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSubject(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const subjectIds = row.subjectId || this.ids;
        this.$confirm('是否确认删除咨询栏目编号为"' + subjectIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSubject(subjectIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      isPermit(permitKey){
        if(this.$store.state.user.name=='admin'){
          return true;
        }
        let  permissions = this.$store.getters.permissions;
        return permissions.includes(permitKey);
      },
    }
  };
</script>
<style type="text/css" scoped>
  .input_w{
    width:300px;
  }
</style>
